<?php
/**
 * (logic)
 */
namespace App\Logic\MonthlyReport;

use App\Models\Basics\SysICountry;
use App\Models\CustomerDelivery\CustomerDelivery;
use App\Models\MonthlyReport\Cost\Delivery\PacketDelivery;
use App\Models\MonthlyReport\Cost\LogisticsBalance;
use App\Models\MonthlyReport\Cost\PaymentV2Order;
use Illuminate\Support\Facades\DB;
class CheckLogisticsSupplement
{
    public static function exe($func,$date)
    {
        try{
            DB::beginTransaction();
            if('null' == $date){
                self::{$func}();
            }else{
                $time = strtotime($date);
                self::{$func}($time);
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
        }
    }

    /**
     * 'AU','AE','MX','BR'订单发货出库
     * 旧版小包发货模块
     * 没有运费的订单：'OPD21031900003','OPD21063000001','OPD21070100001','OPD22041100002','OPD22041500005','OPD22042500002','OPD22120100001','OPD22122800003','OPD23021000001','OPD23021000002','OPD23021300001','OPD23021400003','OPD23021500001','OPD23021600001','OPD23022200001','OPD23022700001','OPD23022700002','OPD23030200001','OPD23030200002','OPD23031300004','OPD23031600001'
     */
    private static function _exePacketDelivery(){
        $orderIds = PaymentV2Order::query()
            ->groupBy('order_id')
            ->pluck('order_id');
        $area = [];
        $pds = PacketDelivery::with('detail')
            ->whereIn('order_no',$orderIds)
            ->where('logistics_status',2)//运费已更新
            ->where('logistics_check_status',0)//运费核算状态正常
            ->orderBy('delivered_at','ASC')
            ->get();
        foreach ($pds as $pd){
            if(!array_key_exists($pd->country_id,$area)){
                $area[$pd->country_id] = SysICountry::getAreaIdByCountryId($pd->country_id);
            }
            $areaId = $area[$pd->country_id];
            foreach ($pd->detail as $pdd){
                if($pdd->ship_num == 0)continue;
                $count = LogisticsBalance::query()
                    ->where('area_id' , $areaId)
                    ->where('psku_id' , $pdd->psku_id)
                    ->where('delivery_no' , $pd->delivery_no)
                    ->count();
                if($count > 0)continue;
                LogisticsBalance::query()->create(
                    [
                        'area_id' => $areaId,
                        'psku_id' => $pdd->psku_id,
                        'delivery_no' => $pd->delivery_no,
                        'delivered_at' => $pd->delivered_at,
                        'b_num' => $pdd->ship_num,
                        'b_price' => $pdd->logistics_fee ?: 0,
                        'b_balance_sale' => $pdd->ship_num,
                    ]
                );
            }
        }
    }


    /**
     * 'AU','AE','MX','BR'订单发货出库的平均物流
     * 新版小包发货模块
     * 没有运费的订单：'OPD23042600001','OPD23042800001','OPD23042300001','OPD23042000001','OPD23041700001','OPD23041800001','OPD23041200002','OPD23041000001','OPD23041100001','OPD23032700002','OPD23032700001','OPD23032300001','OPD23032000001','OPD23032000002','OPD23032100001'
     */
    private static function _exeCustomerPlacing(){
        $orderIds = PaymentV2Order::query()
            ->groupBy('order_id')
            ->pluck('order_id');
        $area = [];
        $pds = CustomerDelivery::with('detail')
            ->whereIn('order_no',$orderIds)
            ->where('delivery_type',3)
            ->where('logistics_status',2)
            ->where('logistics_check_status',0)
            ->orderBy('delivered_at','ASC')
            ->get();
        foreach ($pds as $pd){
            if(!array_key_exists($pd->country_id,$area)){
                $area[$pd->country_id] = SysICountry::getAreaIdByCountryId($pd->country_id);
            }
            $areaId = $area[$pd->country_id];
            foreach ($pd->detail as $pdd){
                if($pdd->ship_num == 0)continue;
                $count = LogisticsBalance::query()
                    ->where('area_id' , $areaId)
                    ->where('psku_id' , $pdd->psku_id)
                    ->where('delivery_no' , $pd->delivery_no)
                    ->count();
                if($count > 0)continue;
                LogisticsBalance::query()->create(
                    [
                        'area_id' => $areaId,
                        'psku_id' => $pdd->psku_id,
                        'delivery_no' => $pd->delivery_no,
                        'delivered_at' => $pd->delivered_at,
                        'b_num' => $pdd->ship_num,
                        'b_price' => $pdd->logistics_fee ?: 0,
                        'b_balance_sale' => $pdd->ship_num,
                    ]
                );
            }
        }
    }
}
